﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VatTuDTO;
using VatTuBUS1;

namespace VatTuBUS
{
    public class GioHangBUS
    {
        public Double TinhTongTienGioHang(List<GioHangDTO> list)
        {
            Double kq = 0;
            if(list != null)
            foreach (GioHangDTO item in list)
            {
                kq = kq + item.ThanhTien;
            }

            return kq;
        }

        public List<GioHangDTO> XoaSanPhamGioHang(List<GioHangDTO> list, int MaSP)
        {
            foreach (GioHangDTO item in list)
            {
                if (item.MaSanPham == MaSP)
                {
                    list.Remove(item);
                    break;
                }
            }

            return list;
        }


        public Double DemSoLuongSanPhamGioHang(List<GioHangDTO> list)
        {
            if (list == null)
                return 0;
            return list.Count;
        }


        public List<GioHangDTO> ThemSanPhamGioHang(List<GioHangDTO> list, int MaSP)
        {

            if (list != null)
            {
                foreach (GioHangDTO item in list)
                {
                    if (item.MaSanPham == MaSP)
                    {
                        item.SoLuong = 1;
                        item.ThanhTien = item.DonGia * item.SoLuong;

                        return list;
                    }
                }
            }

            SanPhamDTO sp_dto = new SanPhamDTO();
            sp_dto = new SanPhamBUS().SelectSanPham(MaSP);

            //DonViTinhDTO dvt_dto = new DonViTinhDTO();
            //dvt_dto = new DonViTinhBUS().LayDonViTinh(sp_dto.MaDonViTinh);


            GioHangDTO cart_dto = new GioHangDTO();
            cart_dto.HinhAnh = sp_dto.HinhAnh;
            cart_dto.TenSanPham= sp_dto.TenSanPham;
            cart_dto.DonGia = sp_dto.GiaBan;
            cart_dto.MaSanPham = sp_dto.MaSanPham;
            cart_dto.SoLuong = 1;
            cart_dto.ThanhTien = sp_dto.GiaBan;
            cart_dto.TenDonViTinh = sp_dto.DonViTinh;
          //  cart_dto.TenDonViTinh = dvt_dto.TenDonViTinh;
            cart_dto.MaLoaiSanPham = sp_dto.MaLoaiSanPham;
            if (list == null)
                list = new List<GioHangDTO>();
            list.Add(cart_dto);

            return list;
        }


        public List<GioHangDTO> CapNhatGioHang(List<GioHangDTO> list, int MaSP,int SoLuong)
        {
            try
            {
                foreach (GioHangDTO item in list)
                {
                    if (item.MaSanPham == MaSP)
                    {
                        if (item.SoLuong == SoLuong)
                            break;

                        item.SoLuong = SoLuong;
                        item.ThanhTien = item.DonGia * item.SoLuong;
                        break;
                    }
                }

                return list;
            }
            catch
            {
                return null;
            }
        }


        public Boolean KiemTra(List<GioHangDTO> list, int MaSP)
        {
            try
            {
                foreach (GioHangDTO item in list)
                {
                    if (item.MaSanPham == MaSP)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
            
        }
       
    }
}
